{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.]]),\n",
       " tensor([[ True, False,  True, False],\n",
       "         [False, False, False, False],\n",
       "         [False, False, False, False]]),\n",
       " tensor([[False, False, False, False],\n",
       "         [ True,  True,  True,  True],\n",
       "         [ True,  True,  True,  True]]),\n",
       " tensor([[False,  True, False,  True],\n",
       "         [False, False, False, False],\n",
       "         [False, False, False, False]]))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#数据操作\n",
    "import torch\n",
    "X = torch.arange(12, dtype=torch.float32).reshape((3,4))\n",
    "Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])\n",
    "X,X<Y, X>Y, X==Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[[0, 1]],\n",
       " \n",
       "         [[2, 3]]]),\n",
       " tensor([[[ 0.,  1.],\n",
       "          [ 2.,  3.],\n",
       "          [ 4.,  5.],\n",
       "          [ 6.,  7.]],\n",
       " \n",
       "         [[ 8.,  9.],\n",
       "          [10., 11.],\n",
       "          [12., 13.],\n",
       "          [14., 15.]]]),\n",
       " tensor([[[ 0.,  2.],\n",
       "          [ 2.,  4.],\n",
       "          [ 4.,  6.],\n",
       "          [ 6.,  8.]],\n",
       " \n",
       "         [[10., 12.],\n",
       "          [12., 14.],\n",
       "          [14., 16.],\n",
       "          [16., 18.]]]))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.arange(16, dtype=torch.float32).reshape((2,4,2))\n",
    "Y=torch.arange(4).reshape((2,1, 2))\n",
    "# Y = torch.tensor([[[1], [ 3]], [[ 5], [ 7]]]) #2*2*1\n",
    "Y,X,X+Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数据预处理\n",
    "import os\n",
    "\n",
    "os.makedirs(os.path.join('..', 'data'), exist_ok=True)\n",
    "data_file = os.path.join('..', 'data', 'house_tiny.csv')\n",
    "with open(data_file, 'w') as f:\n",
    "    f.write('NumRooms,Alley,Price\\n')  # 列名\n",
    "    f.write('NA,Pave,127500\\n')  # 每行表示一个数据样本\n",
    "    f.write('2,wood,106000\\n')\n",
    "    f.write('4,wood,178100\\n')\n",
    "    f.write('NA,NA,140000\\n')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 1, 0]\n",
      "  Alley   Price\n",
      "0  Pave  127500\n",
      "1  wood  106000\n",
      "2  wood  178100\n",
      "3   NaN  140000\n",
      "before inputs\n",
      "   Alley\n",
      "0  Pave\n",
      "1  wood\n",
      "2  wood\n",
      "3   NaN\n",
      "after inputs\n",
      "    Alley_Pave  Alley_wood  Alley_nan\n",
      "0           1           0          0\n",
      "1           0           1          0\n",
      "2           0           1          0\n",
      "3           0           0          1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([[1., 0., 0.],\n",
       "         [0., 1., 0.],\n",
       "         [0., 1., 0.],\n",
       "         [0., 0., 1.]], dtype=torch.float64),\n",
       " tensor([[127500.],\n",
       "         [106000.],\n",
       "         [178100.],\n",
       "         [140000.]], dtype=torch.float64))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import torch\n",
    "data=pd.read_csv(data_file)\n",
    "#获取列名\n",
    "col_names=data.columns.tolist()\n",
    "count=[]#记录每列缺少值个数\n",
    "index=0\n",
    "for col_name in col_names:\n",
    "    count.append(0)\n",
    "    for col in data[col_name].tolist():\n",
    "        # print(type(col))\n",
    "        if str(col)=='nan':\n",
    "            count[index]+=1\n",
    "    index+=1\n",
    "print(count)\n",
    "del_index=count.index(max(count))\n",
    "data=data.drop(data.columns[del_index],axis=1)\n",
    "print(data)\n",
    "inputs=data.iloc[:,0:1]\n",
    "print('before inputs\\n',inputs)\n",
    "outputs=data.iloc[:,1:]\n",
    "inputs=pd.get_dummies(inputs, dummy_na=True)\n",
    "print('after inputs\\n',inputs)\n",
    "X = torch.tensor(inputs.to_numpy(dtype=float))\n",
    "y = torch.tensor(outputs.to_numpy(dtype=float))\n",
    "X, y\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tpp",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
